package com.kancy.learn.rpc.rmi.common.util;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.*;

/**
 * Ip工具类
 *
 * @author kancy
 * @version 1.0
 * @date 2019/1/28 11:16
 */
public class LocalIpUtil {

    /**
     * 获取IP地址
     * @return
     */
    public static String getIp() {
        String ip = null;
        InetAddress inetAddress = getInetAddress();
        if(inetAddress != null){
            ip = inetAddress.getHostAddress();
        }
        return ip;
    }

    /**
     * 获取IP地址
     * @return
     */
    public static String getHostName() {
        String hostName = null;
        InetAddress inetAddress = getInetAddress();
        if(inetAddress != null){
            hostName = inetAddress.getHostAddress();
        }
        return hostName;
    }
    /**
     * 获取 getInetAddress
     * @return
     */
    public static InetAddress getInetAddress() {
        InetAddress ip = null;
        try {
            Enumeration allNetInterfaces = NetworkInterface.getNetworkInterfaces();
            while (allNetInterfaces.hasMoreElements()){
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                Enumeration addresses = netInterface.getInetAddresses();
                while (addresses.hasMoreElements()){
                    ip = (InetAddress) addresses.nextElement();
                    // 排除回环地址和外网IP
                    if (ip != null && !ip.isLoopbackAddress() &&
                            ip.isSiteLocalAddress()&& ip instanceof Inet4Address){
                        return ip;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            // 降级处理
            if(ip == null){
                ip = InetAddress.getLocalHost();
            }
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return ip;
    }

    /**
     * 获取本机所有网卡Ip地址
     * @return
     */
    public static List<String> getLocalIpList() {
        Map<String,String> map = getLocalIpMap();
        List list = new ArrayList();
        list.addAll(map.values());
        return list;
    }
    /**
     * 获取本机所有网卡Ip地址
     * name:ip
     * @return
     */
    public static Map<String,String> getLocalIpMap() {
        Map<InetAddress, NetworkInterface> map = getInetAddressNetworkInterfaces();
        if(map.isEmpty()){
            // 系统未安装网卡
            return null;
        }
        Map<String,String> maps = new HashMap();
        Set<Map.Entry<InetAddress, NetworkInterface>> entries = map.entrySet();
        for (Map.Entry<InetAddress, NetworkInterface> entry:entries) {
            maps.put(entry.getValue().getName(), entry.getKey().getHostAddress());
        }
        return maps;
    }


    /**
     * 获取所有有用的网卡信息
     * @return
     */
    public static Map<InetAddress, NetworkInterface> getInetAddressNetworkInterfaces() {
        InetAddress ip = null;
        Map<InetAddress,NetworkInterface> map = new HashMap();
        try {
            Enumeration<?> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();
                Enumeration<?> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = (InetAddress) ips.nextElement();
                    if ((ip == null) || ("".equals(ip))) {
                        continue;
                    }
                    if ((ip.getHostAddress() == null) || (ip.getHostAddress().indexOf(":") > -1)) {
                        continue;
                    }
                    if(ip.isLoopbackAddress()){
                        continue;
                    }
                    map.put(ip, ni);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

}
